package com.hyj.shop.project.utils.controller;


import com.hyj.shop.project.utils.service.UploadFileService;
import com.hyj.shop.utils.StringUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 *
 * @author ruoyi
 */
@RestController
public class UploadFileController
{
    @RestController
    @RequestMapping("/api")
    public class UploadController {
        @Resource
        UploadFileService uploadImageService;
        @PostMapping(value = "/uploadFile")
        @ResponseBody
        public Map upLoadImage(@RequestParam("file") MultipartFile file) throws IOException {
            System.out.println("拿到前端的文件");
            // 获取文件的名称
            String fileName = file.getOriginalFilename();

            // 使用工具类根据上传文件生成唯一图片名称
            String imgName = StringUtil.getRandomImgName(fileName);

            if (!file.isEmpty()) {

                FileInputStream inputStream = (FileInputStream) file.getInputStream();

                String path = uploadImageService.uploadQNImg(inputStream, imgName);
                System.out.print("七牛云返回的图片链接:" + path);
                Map map = new HashMap();
                Map result = new HashMap();
                result.put("url",path);
                result.put("href",path);
                result.put("alt","网络错误");
                map.put("errno",0);
                ArrayList data = new ArrayList<HashMap>();
                data.add(result);
                map.put("data",data);
                return map;
            }
            Map map = new HashMap();
            map.put("errno",1);
            return map;
        }

    }
}
